package com.sgb.kjwl.viewmodel.manage;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.harisucici.jetpackbase.base.HBaseViewModel;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.chat.NewsNoticeEntity;
import com.sgb.kjwl.model.entity.website.UploadWebsiteBulletinEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.member.MemberConfig;
import com.sgb.member.weight.UploadFileBean;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.LogUtil;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class WebsiteBulletinUploadModel extends HBaseViewModel {

    public MutableLiveData<List<String>> bulletinTypeList = new MutableLiveData<>();
    public MutableLiveData<UploadFileBean> mFileData = new MutableLiveData<>();
    public MutableLiveData<String> resultUploadBulletin = new MutableLiveData<>();

    public WebsiteBulletinUploadModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 获取公告类型数据(临时写死)
     */
    public void getBulletinTypeList() {
        List<String> stringList = new ArrayList<>();
        stringList.add("商品");
        stringList.add("招采");
        stringList.add("集采");
        stringList.add("租赁");
        stringList.add("物流");
        stringList.add("工程");
        stringList.add("企服");
        stringList.add("其他");
        bulletinTypeList.postValue(stringList);
    }

    /**
     * 上传单个文件
     *
     * @param item 上传文件信息类
     */
    public void uploadSingleFile(UploadFileBean item) {
        File file = new File(item.getFilePath());
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"file." + item.getExtension(), requestBody);
        MemberConfig.getInstance().getApi().uploadFile(photos).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                LogUtil.e("文件获取路径", "file===============" + data.getData());
                item.setUrlPath(data.getData());
                mFileData.setValue(item);
            }
        });
    }


    /**
     * 新增网站公告
     *
     * @param entity 所要上传的类
     */
    public void uploadWebsiteBulletin(UploadWebsiteBulletinEntity entity) {
        APIManager.getInstance().getWebsiteInfoAPI().addWebsiteBulletin(entity).enqueue(new ICallBack<BaseEntity>(true, true) {
            @Override
            public void onSuccess(BaseEntity data) {
                resultUploadBulletin.postValue(data.getState());
            }
        });
    }

    /**
     * 根据路由获取H5链接访问
     *
     * @param mContext
     * @param body     传入的参数
     * @param param    路由地址
     * @param code     返回的Code码
     */
    public void getH5Url(Context mContext, String body, String param, int code, String title) {
        DialogHelper.showProgressDialog(mContext, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?title=" + title;
                    WebViewX5HostActivity.start(mContext, url, body, code);
                } else {
                    MToast.showToast(mContext, data.getMsg());
                }
            }
        });
    }
}
